﻿/**********************************************************************************
 * 程序说明：     数据库提供类
 * 创建日期：     2009.10.16
 * 程序制作：     agui
 * 联系方式：     mailto: 3549903 93@qq.com  
 * ********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using PlugNT.Safe;

namespace PlugNT.Database.Common
{
    /// <summary>
    /// 数据库提供类
    /// </summary>
    public abstract class DbProvider
    {
        /// <summary>
        /// ole数据源提供对象
        /// </summary>
        private static DbProviderFactory oleFactory;
        /// <summary>
        /// sql数据源提供对象
        /// </summary>
        private static DbProviderFactory sqlFactory;
        /// <summary>
        /// oracle数据源提供对象
        /// </summary>
        private static DbProviderFactory oracleFactory;
        /// <summary>
        /// mysql数据源提供对象
        /// </summary>
        private static DbProviderFactory mysqlFactory;



        /// <summary>
        /// 得到数据源提供对象(修改此处还要修改Sql.cs中的GetReSetColumnStr函数)
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <returns>DbProviderFactory</returns>
        public static DbProviderFactory GetFactory(DbInfo dbinfo)
        {

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    if (sqlFactory == null)
                    {
                        sqlFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return sqlFactory;
                case "System.Data.OleDb":
                    if (oleFactory == null)
                    {
                        oleFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return oleFactory;
                case "System.Data.OracleClient":
                    if (oracleFactory == null)
                    {
                        oracleFactory = DbProviderFactories.GetFactory(dbinfo.DbType);
                    }
                    return oracleFactory;
                case "MySql.Data.MySqlClient":
                    if (mysqlFactory == null)
                    {
                        mysqlFactory = new MySql.Data.MySqlClient.MySqlClientFactory();
                    }
                    return mysqlFactory;
                default:
                    return null;
            }
        }

        /// <summary>
        /// 得到检查数据库表的sql语句
        /// </summary>
        /// <param name="dbinfo"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static string GetSearchTableSql(DbInfo dbinfo, string TableName)
        {

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    return "select count(1) from sysobjects where id = object_id(N'[" + StringHelper.SqlFilter(TableName) + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
                case "System.Data.OleDb":
                    //(数据库中需要查看权限)工具 -> 选项 -> 视图 -> 隐藏对象、系统对象前面的勾选上。工具 -> 安全 -> 用户与组的权限在对象名称中选定 MSysObjects ，然后权限中设置其权限。
                    return "SELECT Count(1) AS Qty FROM MSysObjects WHERE name='" + StringHelper.SqlFilter(TableName) + "'";    
                case "System.Data.OracleClient":
                    return "";
                default:
                    return "";
            }
        }

        /// <summary>
        /// 创建基类连接对象
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <returns>DbConnection</returns>
        public static DbConnection CreateConnection(DbInfo dbinfo)
        {
            DbConnection connection = GetFactory(dbinfo).CreateConnection();
            connection.ConnectionString = dbinfo.ConnectionString;    //connectionString;
            return connection;
        }

        /// <summary>
        /// 创建Text类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeTextParam(DbInfo dbinfo, string paraName)
        {
            DbParameter dbParam = null;
            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    //DbParameter dbParameter = cmd.CreateParameter();
                    //dbParameter.DbType = dbType;
                    //dbParameter.ParameterName = parameterName;
                    //dbParameter.Direction = ParameterDirection.ReturnValue;
                    //cmd.Parameters.Add(dbParameter);
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.NText);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.VarWChar);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.LongText);
                    break;
                default:
                    break;
            }
            return dbParam;
        }
        
        /// <summary>
        /// 创建Int类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeIntParam(DbInfo dbinfo, string paraName)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    //dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null);
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.Int);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.Integer);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.Int32);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

        /// <summary>
        /// 创建Int类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <param name="size">长度</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeIntParam(DbInfo dbinfo, string paraName,int size)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.Int,size);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.Integer,size);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.Int32,size);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

        /// <summary>
        /// 创建Bit类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeBitParam(DbInfo dbinfo, string paraName)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.Bit, 1);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.BigInt,1);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.Bit,1);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

        /// <summary>
        /// 创建Char类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <param name="size">长度</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeCharParam(DbInfo dbinfo, string paraName, int size)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.Char, size);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.WChar, size);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.VarChar,size);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

        /// <summary>
        /// 创建VarChar类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <param name="size">长度</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeVarCharParam(DbInfo dbinfo, string paraName,int size)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.VarChar, size);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.VarWChar, size);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.VarChar,size);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

        /// <summary>
        /// 创建DateTime类型的Cmd参数
        /// </summary>
        /// <param name="dbinfo">数据库连接信息对象</param>
        /// <param name="paraName">参数名("@argname")</param>
        /// <returns>DbParameter</returns>
        public static DbParameter MakeDateTimeParam(DbInfo dbinfo, string paraName)
        {
            DbParameter dbParam = null;

            switch (dbinfo.DbType)
            {
                case "System.Data.SqlClient":
                    dbParam = new System.Data.SqlClient.SqlParameter(paraName, SqlDbType.DateTime);
                    break;
                case "System.Data.OleDb":
                    dbParam = new System.Data.OleDb.OleDbParameter(paraName, System.Data.OleDb.OleDbType.Date);
                    break;
                case "MySql.Data.MySqlClient":
                    dbParam = new MySql.Data.MySqlClient.MySqlParameter(paraName, MySql.Data.MySqlClient.MySqlDbType.DateTime);
                    break;
                default:
                    break;
            }
            return dbParam;
        }

    }
}
